package oot.activity;


import oot.data.Connection;
import oot.tab.activity.R;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class RegisterActivity extends Activity {
    private EditText editTextUsername;
    private EditText editTextPassword;
    private EditText editTextRepass;
    private EditText editTextEmail;
    private EditText editTextScreenname;
    private Button signup;
    private String username;
    private String password;
    private String repass;
    private String email;
    private String screenname;
    private ProgressDialog progressDialog;
    private JSONObject js;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		editTextUsername = (EditText)findViewById(R.id.username_editText);
		editTextPassword = (EditText)findViewById(R.id.password_editText);
		editTextRepass = (EditText)findViewById(R.id.repassword_editText);
		editTextEmail = (EditText)findViewById(R.id.email_editText);
		editTextScreenname = (EditText)findViewById(R.id.screenname_editText);
		signup = (Button) findViewById(R.id.registerBtt);
		progressDialog = new ProgressDialog(this);
		
		
		editTextUsername.setOnFocusChangeListener(new OnFocusChangeListener() {
	    
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(editTextUsername.getText().toString().length()<1){
					Log.i("Test","edt is empty");
					
					//editTextUsername.requestFocus();
				}
			}
		});
		signup.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.i("Test click signup","Clicked");
				username =editTextUsername.getText().toString();
				password= editTextPassword.getText().toString();
				repass =editTextRepass.getText().toString();
				email = editTextEmail.getText().toString();
				screenname = editTextScreenname.getText().toString();
				if(username.length()==0||password.length()==0||repass.length()==0){
					Log.i("Test click signup","null");
					Toast toast = Toast.makeText(getApplicationContext(),
							"User name or password blank field",
							Toast.LENGTH_SHORT);
					toast.show();
					return;
				}
				
				else if(!password.equals(repass)){
					Log.i("Test click signup","pass not match");
					Toast toast = Toast.makeText(getApplicationContext(),
							"password not matched",
							Toast.LENGTH_SHORT);
					toast.show();
					return;
				}else{
					Log.i("Test click signup","all field ok");
					if(signUp(username, password, email, screenname)){
						
					}else{
						Toast toast = Toast.makeText(getApplicationContext(),
								"not sigup successfull",
								Toast.LENGTH_SHORT);
						toast.show();
						return;
					}
					
				}
			}
		});
	}
    private boolean signUp(String username,String pass,String email,String screenname){
    	RequestParams params = new RequestParams();
		params.put("username", username);
		params.put("password", pass);
		params.put("email", email);
		params.put("screenname", screenname);
		params.put("avatar", "");
		Log.i("test", "params :" + params.toString());
		//Connection.setBaseURL("http://192.168.1.9/");
		Connection.post("users/add", params,
				new AsyncHttpResponseHandler() {
					public void onSuccess(String arg0) {
						Log.i("test", "return --------" + arg0);
						if (progressDialog.isShowing()) {
							progressDialog.dismiss();
						}
						super.onSuccess(arg0);
						try {
							js = new JSONObject(arg0);
							// Log.i("test",)
							if (js.getInt("status_code") != 0) {
								// show error message if error occur
								try {
									Animation shake = AnimationUtils
											.loadAnimation(
													getApplicationContext(),
													R.anim.shake);
									signup.startAnimation(shake);
									Toast toast = Toast.makeText(
											getApplicationContext(),
											js.getString("message"),
											Toast.LENGTH_SHORT);
									toast.show();

								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							} else {
								
								Intent i = new Intent(getApplicationContext(),
										LoginActivity.class);
								//i.putExtra("user", FlashScreenActivity.user);
								startActivity(i);
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(Throwable arg0, String arg1) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, arg1);
						Log.e("Faile","On Faile");
						if(progressDialog.isShowing()){
							progressDialog.dismiss();
							Toast.makeText(getApplicationContext(), "Login faile", Toast.LENGTH_LONG).show();
						}
					}
				});
    	
    	return true;
    }
}
